package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import to.MaterialTO;
import to.ProductTO;

public class ProductParser {
	private static ProductParser instance;
	private ProductParser(){
	}
	
	public static ProductParser getInstance(){
		if(instance==null){
			instance = new ProductParser();
		}
		return instance;
	}
		
	/*
	public void writeProduct(String dir, ProductTO product){
		JAXBContext context = null;
		Marshaller marshaller = null;
		
		try {
			context = JAXBContext.newInstance(MaterialTO.class);
			marshaller = context.createMarshaller();
			marshaller.setProperty(marshaller.JAXB_ENCODING, "utf-8");
			marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			marshaller.marshal(product, new FileWriter("C:\\cloth-factory\\"+ dir + "\\" +product.getProductId()+".xml"));
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public ProductTO readProduct(String dir, int productId){
		ProductTO product = null;
		JAXBContext context = null;
		Unmarshaller unmarshaller = null;
		File file = new File("C:\\cloth-factory\\"+ dir + "\\" + productId + ".xml");
		if(!file.exists()) return null;
		
		try {
			context = JAXBContext.newInstance(ProductTO.class);
			unmarshaller = context.createUnmarshaller();
			product = (ProductTO) unmarshaller.unmarshal(new FileReader(file));
			file.delete();
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return product;
	}
	*/

	public void writeProduct(String dir, ProductTO product){
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		
		try {
			File file = new File("C:\\cloth-factory\\"+ dir + "\\" +product.getProductId()+".pd");
			fos = new FileOutputStream(file);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(product);
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(oos!=null){
				try {
					oos.close();
				} catch (IOException e) {
				}
			}
		}
	}
	
	synchronized public ProductTO readProduct(String dir, int productId){
		ProductTO product = null;
		ObjectInputStream oin = null;
		FileInputStream fin = null;
		File file = new File("C:\\cloth-factory\\"+ dir + "\\" + productId + ".pd");
		if(!file.exists()) return null;
		
		try {
			fin = new FileInputStream(file);
			oin = new ObjectInputStream(fin);
			product = (ProductTO) oin.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			if(oin != null){
				try {
					oin.close();
					file.delete();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return product;
	}
	public boolean existFile(String dir, int productId) {
		return new File("C:\\cloth-factory\\"+ dir + "\\" + productId + ".pd").exists();
	}
}
